import axios from 'axios';
// 设置延迟时间
axios.timeout = 0;
// 设置baseURL
axios.defaults.baseURL = 'http://localhost:3000';

axios.interceptors.request.use(config => config);

axios.interceptors.response.use(
  response => response,
  error => Promise.reject(error)
);

const Request = (options) => {
  const { type, data } = options;
  if (!options.headers) {
    options.headers = {};
  }
  if (!options.method) options.method = 'get'; //默认get
  if (options.method === 'get' && !options.params && data) {
    delete options.data;
    options.params = data;
  }
  let typeTemp = 'json'; //默认json
  if (type != null) {
    typeTemp = type;
  }
  switch (typeTemp) {
    case 'json':
      options.headers['Content-Type'] = 'application/json';
      break;
    case 'formData':
      options.headers['Content-Type'] = 'multipart/form-data';
      break;
    case 'multipart':
      delete options.headers['Content-Type'];
      break;
    default:
  }
  return axios(options)
    .then(res => res)
    .catch(err => Promise.resolve(err));
};
export default Request;
